//
//  OrderController.m
//  RentTable
//
//  Created by mac on 2024/11/28.
//

#import "OrderController.h"
#import "OrderEmptyView.h"
#import "StatusButtonView.h"
#import "OrderTabCell.h"
#import "HomeOrderInfoController.h"
#import "HomeTableDetailController.h"

@interface OrderController ()<StatusButtonViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) StatusButtonView *statusButtonView;
@property (nonatomic, strong) OrderEmptyView *orderEmptyView;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<RentModel *> *orderItem;

@end

@implementation OrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self setUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSArray<RentModel *> *orderItem = [RentModelManager loadRentModels];
    
    self.orderItem = orderItem;
    
    NSString *account = [LocalDataManager loadDataForKey:UserAccount];
    NSString *logStau = [LocalDataManager loadDataForKey:LogStatu];
    if (!account || [logStau isEqualToString:@"LogOut"]) {
        self.orderItem = @[].mutableCopy;
    }
    
    if (self.orderItem.count > 0) {
        if (!self.tableView) {
            [self loadTableView];
        }
        [self.tableView reloadData];
        [self.tableView setHidden:false];
        if (self.orderEmptyView) {
            [self.orderEmptyView setHidden:true];
        }
        
    }else {
        if (!self.orderEmptyView) {
            [self loadEmptyView];
        }
        [self.orderEmptyView setHidden:false];
        if (self.tableView) {
            [self.tableView setHidden:true];
        }
    }
    
}

- (void)setUI {
    UIImageView *playImageView = [UIImageView new];
    [playImageView setUserInteractionEnabled:true];
    playImageView.image = [UIImage imageNamed:@"蒙版分组"];
    [self.view addSubview:playImageView];
    [playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    NSArray<RentModel *> *orderItem = [RentModelManager loadRentModels];
    self.orderItem = orderItem;
    
    NSString *account = [LocalDataManager loadDataForKey:UserAccount];
    NSString *logStau = [LocalDataManager loadDataForKey:LogStatu];
    if (!account || [logStau isEqualToString:@"LogOut"]) {
        self.orderItem = @[].mutableCopy;
    }
    
    [self loadSelectButton];
    if (self.orderItem.count > 0) {
        [self loadTableView];
    }else {
        [self loadEmptyView];
    }
    
    
}

- (void)loadSelectButton {
    self.statusButtonView = [[StatusButtonView alloc] initWithFrame:CGRectMake(16, 76 * scaleH, spDeviceWidth - 32, 40)];
    self.statusButtonView.delegate = self;
    [self.view addSubview:self.statusButtonView];
}

- (void)loadEmptyView {
    self.orderEmptyView = [OrderEmptyView new];
    [self.view addSubview:self.orderEmptyView];
    [self.orderEmptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(0);
        make.top.mas_equalTo(116 * scaleH);
    }];
    QLWEAKSELF
    self.orderEmptyView.orderSelect = ^(RentModel * _Nonnull rentModel) {
        HomeTableDetailController *vc = [HomeTableDetailController new];
        vc.rentModel = rentModel;
        vc.hidesBottomBarWhenPushed = true;
        [weakSelf.navigationController pushViewController:vc animated:true];
    };
}

- (void)loadTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[OrderTabCell class] forCellReuseIdentifier:@"OrderTabCell"];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.trailing.mas_equalTo(-16);
        make.top.equalTo(self.statusButtonView.mas_bottom).offset(20 * scaleH);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableView reloadData];
}


- (void)didSelectButtonWithTitle:(NSString *)title {
    NSLog(@"%@",title);
    
    if ([title isEqualToString:@"审核中"]) {
        if (self.orderItem.count > 0) {
            [self.tableView setHidden:false];
            [self.orderEmptyView setHidden:true];
        }
    }else {
        [self.tableView setHidden:true];
        if (!self.orderEmptyView) {
            [self loadEmptyView];
        }
        [self.orderEmptyView setHidden:false];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orderItem.count;
}

- (__kindof UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderTabCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    RentModel *model = self.orderItem[indexPath.row];
    cell.rentModel = model;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeOrderInfoController *vc = [HomeOrderInfoController new];
    RentModel *model = self.orderItem[indexPath.row];
    vc.rentModel = model;
    vc.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:vc animated:true];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}


@end
